import {
  getDomainStats,
  getDifficultyStats,
  getReviewStatusStats,
} from "~/server/database/services/topicService";
import type { TopicQueryParams } from "~/server/database/services/topicService";

export default defineEventHandler(async (event) => {
  try {
    // 获取查询参数
    const query = getQuery(event);
    
    // 构建筛选参数
    const params: TopicQueryParams = {};
    if (query.domain) params.domain = query.domain as string;
    if (query.difficulty) params.difficulty = query.difficulty as string;
    if (query.reviewStatus) params.reviewStatus = query.reviewStatus as string;
    if (query.submitterId) params.submitterId = parseInt(query.submitterId as string);
    if (query.search) params.search = query.search as string;

    // 获取领域统计、难度统计和审核状态统计
    const [domainStats, difficultyStats, reviewStatusStats] = await Promise.all([
      getDomainStats(params),
      getDifficultyStats(params),
      getReviewStatusStats(params),
    ]);

    return {
      success: true,
      data: {
        domainStats,
        difficultyStats,
        reviewStatusStats,
      },
    };
  } catch (error) {
    console.error("获取统计信息失败:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "获取统计信息失败",
    });
  }
});
